package com.smart4j.framework.util;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

import lombok.extern.slf4j.Slf4j;

/**
 * 编码解码操作工具类
 *
 * @author 段誉
 * @create 2019-04-01 18:54
 */
@Slf4j
public final class CodeUtil {
  /**
   * 将url编码
   */
  public static String encodeURL(String source) {
    String target;
    try {
      target = URLEncoder.encode(source, "UTF-8");
    } catch (UnsupportedEncodingException e) {
      log.error("encode url failure", e);
      throw new RuntimeException(e);
    }
    return target;
  }

  /**
   * 将URL解码
   */
  public static String decodeURL(String source) {
    String target;
    try {
      target = URLDecoder.decode(source, "UTF-8");
    } catch (UnsupportedEncodingException e) {
      log.error("decode url failure", e);
      throw new RuntimeException(e);
    }
    return target;
  }
}
